package cn.otrue.patienthealthmanager.adapter;

import java.io.File;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import cn.otrue.patienthealthmanager.R;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MainDataInfoMainDataInfoActivityAdpater extends BaseAdapter {
	public void ImageSet(String imgurl, ImageView img_headPhoto) {

		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_user_photo)
				.showImageOnFail(R.drawable.iv_user_photo)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.build();
		ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	}
	private List<Map<String,String>> goods;
	private LayoutInflater inflater;
	private Context context;
	
	
	public MainDataInfoMainDataInfoActivityAdpater(List<Map<String,String>> goods,
			Context context ) {
		this.goods = goods;
		inflater = LayoutInflater.from(context);
		this.context = context;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return goods.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return goods.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = inflater
				.inflate(R.layout.item_lishi_tupian, null);
		
		ImageView pe_head_image = (ImageView) convertView.findViewById(R.id.iv_lishitupian);
String url ="";
		if (goods.size()!=0) {
			Map<String,String> eachMap = goods.get(position);
			
			if (eachMap.containsKey("picname")) {
				url =	 eachMap.get("picname").toString();
				
			}else{
				 url = eachMap.get("imagepic").toString();
				
			}
			
			
			final File path = ImageLoader.getInstance().getDiscCache().get(url).getAbsoluteFile();
			ImageSet(url,pe_head_image);
			pe_head_image.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();
					intent.setAction(android.content.Intent.ACTION_VIEW);
					intent.setDataAndType(
							Uri.fromFile(path),
							"image/*");
					context.startActivity(intent);

				}
			});


		}
		
		return convertView;
	}

}
